package cn.wp.ewallet.wmps.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.wp.ewallet.wmps.dao.FinaProdInfoDao;
import cn.wp.ewallet.wmps.dao.ProdOrderRelDao;
import cn.wp.ewallet.wmps.extension.constant.EwalletWmpsConstant;
import cn.wp.ewallet.wmps.model.entity.FinaProdInfoDo;
import cn.wp.ewallet.wmps.model.entity.ProdOrderRelDo;
import cn.wp.ewallet.wmps.service.ProdOrderRelChangeStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProdOrderRelChangeStatusServiceImpl implements ProdOrderRelChangeStatusService {

    @Autowired
    private ProdOrderRelDao prodOrderRelDao;

    @Autowired
    private FinaProdInfoDao finaProdInfoDao;

    @Override
    public void changeStatus() {
      //查看所有的带计息的理财订单列表
        List<ProdOrderRelDo> prodOrderRelDoList = prodOrderRelDao.selectListByOrderStatus(EwalletWmpsConstant.ORDER_STATUS_WAITING);

        for (int i = 0; i < prodOrderRelDoList.size(); i++) {
            ProdOrderRelDo prodOrderRelDo = prodOrderRelDoList.get(i);
            Date effDate = prodOrderRelDo.getEffDate();
            //判断当日是不是生效日
            boolean flag= judgeDateEuqels(new Date(), effDate);
            if(flag){
                prodOrderRelDo.setOrderStatus(EwalletWmpsConstant.ORDER_STATUS_BEARING);
                FinaProdInfoDo finaProdInfoDo = finaProdInfoDao.selectFinaProdInfoById(prodOrderRelDo.getFinaProdId());
                if(finaProdInfoDo.getProdType().equals(EwalletWmpsConstant.PROD_TYPE_DAILY)){
                    prodOrderRelDo.setOrderStatus(EwalletWmpsConstant.ORDER_STATUS_END);
                }
                prodOrderRelDo.setStatusTime(new Date());
                prodOrderRelDao.updateProdOrderRelDo(prodOrderRelDo);
            }
        }

    }

    public boolean judgeDateEuqels(Date date1, Date date2) {
        String dateFormatDate1 = DateUtil.formatDate(date1);
        String dateFormatDate2 = DateUtil.formatDate(date2);
        return dateFormatDate1.equals(dateFormatDate2);
    }
}
